package cn.samir.sudoku.core;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.util.LogUtils;
import cn.samir.sudoku.util.Utils;

public class InputItemView extends FrameLayout {

	
	public static final int MARK_ENABLE_STATUS_COLOR =0XFFA500;
	public static final int MARK_DISABLE_STATUS_COLOR =0;
	int number;
	int status;
	InputItem mInputItem;
	TextView numberTextView;
	ImageView imageView;
	View view;
	int mWidth;
	int mHeight;

	public InputItemView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		inits();
	}

	private void inits() {
		view = View.inflate(getContext(), R.layout.inputwindow_number, null);
		numberTextView = Utils.getView(view, R.id.tv_number);
		imageView = Utils.getView(view, R.id.iv_status);
		addView(view);
	}

	public InputItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		inits();

	}

	public InputItemView(Context context) {
		super(context);
		inits();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//drawBorder(canvas);
	}

	public void drawBorder(Canvas canvas) {
		Paint mBorderPaint = new Paint();
		mBorderPaint.setStrokeWidth(1f);
		mBorderPaint.setColor(Color.BLACK);
		mHeight = getHeight();
		mWidth = getWidth();
		canvas.drawLine(0, mHeight, mWidth, mHeight, mBorderPaint);
		canvas.drawLine(0, 0, mWidth, 0, mBorderPaint);
		canvas.drawLine(mWidth, 0, mWidth, mHeight, mBorderPaint);
		canvas.drawLine(0, 0, 0, mHeight, mBorderPaint);
		Log.d(VIEW_LOG_TAG, "InputItemView drawBorder");
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public InputItem getmInputItem() {
		return mInputItem;
	}

	public void setmInputItem(InputItem mInputItem) {
		this.mInputItem = mInputItem;
//		TextDrawable drawable = TextDrawable.builder().beginConfig()
//				.fontSize(22).bold().textColor(Color.BLACK).endConfig()
//				.buildRect("" + mInputItem.number, Color.WHITE);
//		numberImageView.setImageDrawable(drawable);
		LogUtils.i(VIEW_LOG_TAG, mInputItem.toString());
		if(Utils.isNumberLegal(number)){
			numberTextView.setText(mInputItem.number+"");
		}else{
			numberTextView.setText(mInputItem.text+"");
		}
		
		if (mInputItem.status == 0) { //0不显示，1显示，2 灰色
			imageView.setVisibility(View.INVISIBLE);
		} else {
			imageView.setVisibility(View.VISIBLE);
			if(mInputItem.status == 1){// 
				// 这个标记可以标记到方框中，反之不能
				imageView.setBackgroundColor(getResources().getColor(R.color.mark_disable_color));
			}else if(mInputItem.status == 2){
				imageView.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
			}
		}
	    invalidate();
	}

}
